package cn.jiedh.app.meiweidk.Utils.tool;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.lang.ref.WeakReference;
import java.util.Locale;

/**
 * @name：
 * @author： liuyun
 * @phone： 15723310658
 * @createTime： 2017/7/21
 * @modifyTime： 2017/7/21
 * @explain：说明
 */


public class GlobalStateMgr {
    private static final String TAG = "GlobalStateMgr";
    private static final int MAX_VER_COUNT = 4;
    private static String sVersionName;
    private static int sVersionCode;
    private static WeakReference<Activity> sCurrentTopActivity;
    private static String sCurrentTopActivityName;
    private static boolean sIsForeGround;
    private static boolean sIsExited = false;

    private static void loadVersionIfNeeded(Context cxt) {
        if (sVersionName == null) {
            PackageManager manager = cxt.getPackageManager();
            int versionCode = -1;
            try {
                PackageInfo info = manager.getPackageInfo(cxt.getPackageName(), 0);
                sVersionCode = info.versionCode;
                sVersionName = info.versionName;
            } catch (PackageManager.NameNotFoundException e) {
            }
        }
    }

    public static int getVersionCode(Context cxt) {
        loadVersionIfNeeded(cxt);
        return sVersionCode;
    }

    public static String getVersionName(Context cxt) {
        loadVersionIfNeeded(cxt);
        return sVersionName;
    }

    public static String getCustomUserAgent(Context cxt) {
        return "Terminus/" + sVersionName + " " + "lan/" + getLanguage(cxt);
    }

    public static Activity getCurrentTopActivity() {
        return sCurrentTopActivity != null ? sCurrentTopActivity.get() : null;
    }

    public static void setCurrentTopActivity(Activity activity) {
        if (activity != null) {
            sCurrentTopActivity = new WeakReference<Activity>(activity);
        } else {
            sCurrentTopActivity = null;
        }
    }

    public static void setCurrentTopActivityName(String activityName) {
        sCurrentTopActivityName = activityName;
    }

    public static String getCurrentTopActivityName() {
        return sCurrentTopActivityName;
    }



    public static boolean isExited() {
        return sIsExited;
    }

    public static void setIsExited(boolean isExited) {
        sIsExited = isExited;
    }

    public static boolean isForeGround() {
        return sIsForeGround;
    }

    public static String getLanguage(Context context) {
        Locale locale = context.getResources().getConfiguration().locale;
        String language = locale.getLanguage().toLowerCase(locale);
        String country = locale.getCountry().toLowerCase(locale);
        if (TextUtils.isEmpty(country)) {
            return language;
        } else {
            return language + "_" + country;
        }
    }

    public static String getIMEI(Context context) {
        TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (manager != null) {
            return manager.getDeviceId();
        }
        return "";
    }
}
